package mylist;

import org.omg.PortableInterceptor.USER_EXCEPTION;

import java.util.Arrays;

public class List implements MyArrayList{

    private int[] elements;
    private int usedSize;
    //顺序表的默认大小
    private static final int DEFAULT_SIZE = 2;

    public List() {
        this.elements = new int[DEFAULT_SIZE];
    }
    public List(int capacity) {
        this.elements = new int[capacity];
    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elements[i]+" ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        /*if(this.usedSize==this.elements.length) {
            return true;
        }
        return false;*/
        return this.elements.length==usedSize;
    }
    private void isEnlarger() {
        if(isFull()) {
            elements = Arrays.copyOf(elements,elements.length*2);
        }
    }
    @Override
    public void add(int data) {
        isEnlarger();
        this.elements[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
        } catch (PosIllegal e) {
            e.printStackTrace();
            return;
        }
        isEnlarger();
        int move = usedSize;
        while (move-1>=pos) {
            elements[move] = elements[move-1];
            move--;
        }
        elements[pos] = data;
        usedSize++;
    }


    private void checkPos(int pos) throws PosIllegal{
        if(pos<0||pos>usedSize) {
            throw new PosIllegal("插入下标异常: "+pos);
        }
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty()) {
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            //查找引用类型,要重写方法
            if(toFind==elements[i]) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isEmpty() {
        return usedSize==0;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()) {
            return -1;
        }
        for (int i = 0; i < usedSize; i++) {
            //查找引用类型,要重写方法
            if(toFind==elements[i]) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        try {
            checkPosOnGet(pos);
        } catch (PosIllegal e) {
            e.printStackTrace();
        }

        return elements[pos];
    }
    private void checkPosOnGet(int pos) throws PosIllegal{
        if(pos<0||pos>=usedSize) {
            throw new PosIllegal("获取指定下标异常: "+pos);
        }
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkPosOnGet(pos);
        } catch (PosIllegal e) {
            e.printStackTrace();
            return;
        }
        elements[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if(indexOf(toRemove)==-1) {
            System.out.println("无该数值可以删除");
            return;
        }
        int i = indexOf(toRemove);
        while (i+1< usedSize) {
            elements[i] = elements[i+1];
            i++;
        }

        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        this.usedSize = 0;
    }

}
